package com.superm.service.impl;

import com.superm.entity.Emp;
import com.superm.entity.Goods;
import com.superm.entity.Order;
import com.superm.mappers.GoodsMapper;
import com.superm.mappers.OrderMapper;
import com.superm.mappers.StorgageRackMapper;
import com.superm.service.OrderService;
import com.superm.utils.EmpContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private StorgageRackMapper storgageRackMapper;
    @Override
    public List<Order> allOrders(Integer empid) {
        return orderMapper.allOrders(empid);
    }

    @Override
    public Map<String, Object> addOrders(Order order) {
        Emp emp = EmpContent.EmpUtils();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("success",false);
        map.put("msg","添加失败");
        order.setGoodstotlePrice(order.getGoodsCount()*order.getGoodsPrice());
        order.setEmpid(emp.geteId());
        Goods goods1 = new Goods();
        System.out.println("3xxxxxxxxxxxxxxxxxx");
        System.out.println(goods1);
        goods1.setGoodsId(order.getGoodsId());
        //减掉商品的数量 以确保唯一性
        goodsMapper.upGoodsCount(order.getGoodsId(),order.getGoodsCount());
        System.out.println("1xxxxxxxxxxxxxxxxxx");
        //减少货架数量
        Goods goods = goodsMapper.allGoods(goods1);
        System.out.println("345txxxxxxxxxxxxxxxxxx");
        Double goodsCapacity = goods.getCapacity()*order.getGoodsCount();//得到下单商品的占用容量
        System.out.println("rtytrexxxxxxxxxxxxxxxxxx");
        storgageRackMapper.decreaseCaoacity(goods.getStorgageRackId(),goodsCapacity);//减少货架的容量
        if(orderMapper.allOrdergoodsid(order.getGoodsId())>0){
            if(orderMapper.upOrder(order)>0){
                map.put("success",true);
                map.put("msg","添加成功");
            }
        }else{
            if(orderMapper.addOrder(order)>0) {
                map.put("success", true);
                map.put("msg", "添加成功");
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> delOrder(Order order) {
        System.out.println(order);
        Map<String, Object> map = new HashMap<>();
        Goods goods1 = new Goods();
        goods1.setGoodsId(order.getGoodsId());
        //减少货架数量
        Goods goods = goodsMapper.allGoods(goods1);
        Double goodsCapacity = goods.getCapacity()*order.getGoodsCount();//得到下单商品的占用容量
        //先增加商品的数量
        goodsMapper.upGoodsCountJ(order.getGoodsId(),order.getGoodsCount());
        storgageRackMapper.minusCaoacity(goods.getStorgageRackId(),goodsCapacity);
        //然后在删除暂时订单
       if(orderMapper.delOrder(order.getOrderid())>0){
           map.put("success", true);
           map.put("msg", "删除成功");
       }else{
           map.put("success", false);
           map.put("msg", "删除失败");
       }
        return map;
    }
    @Override
    public List<Order> allGoodsids(String goodsid, Integer empid) {
        return orderMapper.allGoodsids(goodsid,empid);
    }

    @Override
    public Integer delOrders(String goodsid, Integer empid) {
        return orderMapper.delOrders(goodsid,empid);
    }
}
